package com.example.smart.controller;

import com.example.common.util.R;
import com.example.smart.service.OssService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping(value = "/oss/")
@Tag(name = "上传图像")
public class OssController {
    @Autowired
    private OssService ossService;

    @PreAuthorize("hasAuthority('oss:uploadFile')")
    @Operation(summary = "图像上传")
    @PostMapping(value="/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R uploadFile(@RequestPart(value = "file") MultipartFile file) {
        //调用业务方法
        String url = ossService.uploadFile(file);
        if (url != null) {
            return R.ok("图像上传成功").data("url", url);
        } else {
            return R.error("图像上传失败");
        }
    }
}
